package com.lyy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.lyy.entity.Episode;
import com.lyy.Vo.EpisodeVo;
import com.lyy.Vo.EpisodeVoPage;
import com.lyy.entity.param.EpisodeInsertParam;
import com.lyy.entity.param.EpisodeUpdateParam;
import com.lyy.result.Result;
import com.lyy.service.EpisodeService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Data
@ResponseBody
@RequestMapping("/api/v1/episode")
public class EpisodeController {
    public EpisodeService episodeService;
    @RequestMapping("/select-by-episode-id/{episode-id}")
    public Result selectByChapterId(@PathVariable("episode-id") Integer episodeId){
        Episode episode=episodeService.selectByEpisodeId(episodeId);
        if (ObjectUtil.isEmpty(episode)) {
            return Result.failure();
        }
        EpisodeVo episodeVo=new EpisodeVo();
        BeanUtil.copyProperties(episode, episodeVo);
        return Result.success(episodeVo);
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody EpisodeInsertParam episodeInsertParam){
        Episode episode=BeanUtil.copyProperties(episodeInsertParam,Episode.class);
        int result =episodeService.insertByEpisodeId(episode);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/update")
    public Result update(@RequestBody EpisodeUpdateParam episodeUpdateParam){
        Episode episode=BeanUtil.copyProperties(episodeUpdateParam,Episode.class);
        int result=episodeService.updateByEpisodeId(episode);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/list")
    public Result list(){
        List<Episode> episodes=episodeService.selectAllEpisode();
        if (ObjectUtil.isEmpty(episodes)) {
            return Result.failure();
        }
        List<EpisodeVo> episodeVos = BeanUtil.copyToList(episodes, EpisodeVo.class);
        return Result.success(episodeVos);
    }
    @RequestMapping("/delete-by-episode-id/{episode-id}")
    public Result deleteByEpisodeId(@PathVariable("episode-id") Integer episodeId) {
        int result = episodeService.deleteByEpisodeId(episodeId);
        return result > 0 ? Result.success() : Result.failure();
    }
    @RequestMapping("/delete-by-episode-ids")
    public Result deleteByBannerIds(@RequestParam("episode-ids") Integer[] episodeIds) {
        int result = episodeService.deleteByEpisodeIds(List.of(episodeIds));
        return result > 0 ? Result.success() : Result.failure();
    }
    @RequestMapping("/page")
    public Result page(@RequestParam("page") Integer page,
                       @RequestParam("size") Integer size) {
        PageInfo<Episode> result = episodeService.selectBypage(page, size);
        if (ObjectUtil.isEmpty(result)) {
            return Result.failure();
        }
        EpisodeVoPage episodeVoPage =new EpisodeVoPage();
        episodeVoPage.setPage(result.getPageNum());
        episodeVoPage.setSize(result.getPageSize());
        episodeVoPage.setTotal(result.getTotal());
        episodeVoPage.setPages(result.getPages());
        episodeVoPage.setEpisodeVos(BeanUtil.copyToList(result.getList(), EpisodeVo.class));
        return Result.success(episodeVoPage);
    }


}
